package gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;

import function.Car;
import function.Queue;
import function.Stack;
//此类用于实现对停车场和便道的模拟
public class ParkAndSidewalk extends JFrame {
	private JPanel parkAndSidewalkPanel;
	//停车1s收费1元
	private static long charge = 1;

	public ParkAndSidewalk() {
		super("停车场车辆进出信息记录");
		setSize(1000, 800);
		setLocation(400, 150);

		Font font = new Font("宋体", Font.PLAIN, 25);
		UIManager.put("OptionPane.font", font);
		UIManager.put("OptionPane.messageFont", font);
		UIManager.put("OptionPane.buttonFont", font);

		setVisible(true);
	}

	public void parkAndSidewalkPanel() {
		parkAndSidewalkPanel = new JPanel();
		parkAndSidewalkPanel.setLayout(null);
		Date date = new Date();
		//用大写字母表示汽车
		String[] name = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K" };
		Car[] carlist = new Car[11];
		for (int i = 0; i < carlist.length; i++) {
			carlist[i] = new Car(name[i]);
		}
		Stack<Car> park = new Stack(5);//假设停车场只有5个停车位，用栈来模拟停车场
		Queue<Car> sidewalk = new Queue();//用队列模拟便道
		//先将所有汽车放入一个队列中保存，模拟停车时从此队列中拿车
		Queue<Car> cars = new Queue(carlist);

		JLabel parkLabel = new JLabel("停车位");
		parkLabel.setBounds(50, 50, 100, 30);
		parkLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		parkAndSidewalkPanel.add(parkLabel);

		ButtonGroup group0 = new ButtonGroup();

		JLabel sidewalkLabel = new JLabel("便道");
		sidewalkLabel.setBounds(250, 50, 200, 30);
		sidewalkLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		parkAndSidewalkPanel.add(sidewalkLabel);

		ButtonGroup group1 = new ButtonGroup();
		/*用户每按一次停入按钮，若停车场内有位置，就将车停入停车场
		 * 若无停车位就停到便道上
		 */
		JButton buttonIn = new JButton("停入");
		buttonIn.setBounds(80, 700, 100, 40);
		buttonIn.setFont(new Font("宋体", Font.PLAIN, 25));
		parkAndSidewalkPanel.add(buttonIn);
		buttonIn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//从保存车的队列中出队列一辆车用于模拟停入操作
				Car car = cars.deQueue();
				if (park.isFull()) {    //若停车场满则停在便道上等候，并弹出窗口提示用户
					JOptionPane.showMessageDialog(null,
							car.getMark() + " 停入 " + Integer.toString(sidewalk.size() + 1) + " 便道", "提示",
							JOptionPane.ERROR_MESSAGE);
					//将停入结果显示在界面上
					JRadioButton rb = new JRadioButton(Integer.toString(sidewalk.size() + 1) + ":" + car.getMark(),
							true);
					rb.setBounds(250, 100 + 80 * sidewalk.size(), 100, 30);
					rb.setFont(new Font("宋体", Font.PLAIN, 25));
					parkAndSidewalkPanel.add(rb);
					group1.add(rb);
					sidewalk.enQueue(car);
				} else {//停车场有空位则按顺序停入停车场
					JOptionPane.showMessageDialog(null,
							car.getMark() + " 停入 " + Integer.toString(park.getTop() + 2) + " 停车位", "提示",
							JOptionPane.ERROR_MESSAGE);
					JRadioButton rb = new JRadioButton(Integer.toString(park.getTop() + 2) + ":" + car.getMark(), true);
					rb.setBounds(50, 100 + 80 * (park.getTop() + 1), 100, 30);
					rb.setFont(new Font("宋体", Font.PLAIN, 25));
					parkAndSidewalkPanel.add(rb);
					group0.add(rb);
					Date date = new Date();
					//设置此车的停入时间
					car.setInTime(date.getTime());
					park.push(car);
				}
			}
		});
		/*用户首先通过车辆前的按钮选择一辆停车场内的车
		 * 然后点击开出按钮，即可开始模拟车辆开出过程
		 * 同时计算停车时间和应缴纳的费用
		 */
		JButton buttonSaveAdd = new JButton("开出");
		buttonSaveAdd.setBounds(230, 700, 100, 40);
		buttonSaveAdd.setFont(new Font("宋体", Font.PLAIN, 25));
		parkAndSidewalkPanel.add(buttonSaveAdd);

		buttonSaveAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
		//temp用于存储要开出的车和暂时退出停车场让路的汽车，temp的栈顶就是要开出的车
				Stack<Car> temp = new Stack(5);
			//begin用于标识
				boolean begin = false;
				Car p;
				//获取buttongroup中选中按钮的文本
				Enumeration<AbstractButton> radioBtns = group0.getElements();
				AbstractButton btn;
				//若某项被选中，将该项和后续汽车移除并存储在栈temp中
				while (radioBtns.hasMoreElements()) {
					btn = radioBtns.nextElement();
					if (btn.isVisible() == true) {
						if (begin) {
							p = park.pop();
							temp.push(p);
							btn.setVisible(false);
						} else {
							if (btn.isSelected()) {
								begin = true;
								p = park.pop();
								temp.push(p);
								btn.setVisible(false);
							}
						}
					}
				}
				Car car, outcar;
				outcar = temp.pop();//弹出temp栈顶的车，此车即为要开出的车
				Date date = new Date();
				outcar.setOutTime(date.getTime());//出停车场的时间
				//计算该车在停车场内停留的时间,单位化为秒
				long time = (outcar.getOutTime() - outcar.getInTime())/1000;
				long cost =time * charge;//计算应缴纳的费用，1秒1元钱
				//输出汽车在停车场内停留的时间和应缴纳的费用
				JOptionPane.showMessageDialog(null, outcar.getMark() + " out\n" + "停车时间（单位/秒）:" + time + "应缴纳的费用:" + cost+" 元",
						"提示", JOptionPane.ERROR_MESSAGE);

				int s=park.getTop();
				int t = temp.getTop();
				//将temp中的其余让路车辆停回停车场
				for (int i = 0; i <= t; i++) {
					car = temp.pop();
					park.push(car);
					//将停回的车辆重新显示在界面上
					JRadioButton rb = new JRadioButton(Integer.toString(s + 2 + i) + ":" + car.getMark(), true);
					rb.setBounds(50, 100 + 80 * (s + 1 + i), 100, 30);
					rb.setFont(new Font("宋体", Font.PLAIN, 25));
					parkAndSidewalkPanel.add(rb);
					group0.add(rb);
				}
//				temp2队列用于实现将便道上等候的汽车按顺序停入停车场
				Queue<Car> temp2 = new Queue();
				//获取buttongroup中选中按钮的文本
				Enumeration<AbstractButton> radioBtns1 = group1.getElements();
				AbstractButton btn1;
				while (radioBtns1.hasMoreElements()) {
					btn1 = radioBtns1.nextElement();
					btn1.setVisible(false);
				}
				int ss = sidewalk.size();
					//将sidewalk中的所有元素放入temp2队列中
				for (int i = 0; i < ss; i++) {
					temp2.enQueue(sidewalk.deQueue());
				}
				//若temp2不为空，说明便道上有车在等候停车位，此时将temp2队首元素停入park
				if (!temp2.isEmpty()) {
					Car car3 = temp2.deQueue();
					JRadioButton rb = new JRadioButton(Integer.toString(park.getTop() + 2) +":"+ car3.getMark(), true);
					rb.setBounds(50, 100 + 80 * (park.getTop() + 1), 100, 30);
					rb.setFont(new Font("宋体", Font.PLAIN, 25));
					parkAndSidewalkPanel.add(rb);
					group0.add(rb);
					JOptionPane.showMessageDialog(null,
							car3.getMark() + " 停入 " + Integer.toString(park.getTop() + 2) + " 停车位", "提示",
							JOptionPane.ERROR_MESSAGE);
					park.push(car3);
					Date date1 = new Date();
					car3.setInTime(date1.getTime());
				}

				Car car2;
				int r = temp2.size();
				//将temp2中其余车辆放回sidewalk
				for (int i = 0; i < r; i++) {
					car2 = temp2.deQueue();
					JRadioButton rb2 = new JRadioButton((i + 1) + ":" + car2.getMark(), true);
					rb2.setBounds(250, 100 + 80 * i, 100, 30);
					rb2.setFont(new Font("宋体", Font.PLAIN, 25));
					parkAndSidewalkPanel.add(rb2);
					group1.add(rb2);
					sidewalk.enQueue(car2);
				}
			}
		});

		parkAndSidewalkPanel.setVisible(true);
		add(parkAndSidewalkPanel);
		setVisible(true);
	}
}
